package com.st.mall.order.service.Impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.st.mall.order.mapper.IOrderProMapper;
import com.st.mall.order.model.OrderProModel;
import com.st.mall.order.service.IOrderProService;
import com.st.mall.product.mapper.IProductMapper;
import com.st.mall.product.model.ProductModel;
import com.st.mall.scart.mapper.IScartMapper;
import com.st.mall.scart.model.ScartModel;

@Service
public class OrderProServiceImpl implements IOrderProService{

	@Autowired
	private IOrderProMapper mapper;
	@Autowired
	private IScartMapper scartMapper;
	@Autowired
	private IProductMapper proMapper;

	@Override
	public int settlement(ScartModel scart,String orderCode) {
		String proCode = scart.getProCode();
		ScartModel scartModel = scartMapper.selectOne(scart);
		OrderProModel orderProModel = new OrderProModel();
		orderProModel.setOrderCode(orderCode);
		orderProModel.setProCode(proCode);
		orderProModel.setNumb(scartModel.getNumb());
		orderProModel.setPrice(scartModel.getPrice());
		
		int a = mapper.insert(orderProModel);
		int b = scartMapper.updateStatus(scart);
		if(a==1&&b==1)
			return 1;
		else
			return 2;
	}

	@Override
	public List<OrderProModel> selectAll(OrderProModel orderProModel) {
		List<OrderProModel> list = mapper.selectAll(orderProModel.getOrderCode());
		for(int i=0;i<list.size();i++) {
			String proCode = list.get(i).getProCode();
			ProductModel product = proMapper.selectId(proCode);
			list.get(i).setProName(product.getName());
			list.get(i).setUrl(product.getUrl());
		}
		return list;
	}

	@Override
	public int delete(OrderProModel orderProModel) {
		// TODO Auto-generated method stub
		return mapper.delete(orderProModel);
	}

	@Override
	public OrderProModel selectId(OrderProModel orderProModel) {
		// TODO Auto-generated method stub
		return mapper.selectId(orderProModel);
	}

	@Override
	public int insert(OrderProModel orderProModel) {
		// TODO Auto-generated method stub
		return mapper.insert(orderProModel);
	}

	@Override
	public int update(OrderProModel orderProModel) {
		// TODO Auto-generated method stub
		return mapper.update(orderProModel);
	}

	@Override
	public int selectCount(OrderProModel orderProModel) {
		// TODO Auto-generated method stub
		return mapper.selectCount(orderProModel);
	}

	@Override
	public List<OrderProModel> selectModel(OrderProModel orderProModel) {
		// TODO Auto-generated method stub
		String orderCode = orderProModel.getOrderCode();
		if (orderCode != null) {
			orderProModel.setOrderCode("%" + orderCode + "%");
		}
		String proCode = orderProModel.getProCode();
		if (proCode != null) {
			orderProModel.setProCode("%" + proCode + "%");
		}
		return mapper.selectModel(orderProModel);
	}
	
	
}
